from marshmallow import Schema, fields, validate, pre_load

from constant.CommonConstant import SORT_ORDER_ASC, SORT_ORDER_DESC


class OptionSchema(Schema):
    result = fields.Str(
        required=True,
        allow_none=True
    )
    score = fields.Int(
        required=True,
        allow_none=True,
    )
    value = fields.Str(
        required=True,
        allow_none=False
    )
    key = fields.Str(
        required=True,
        allow_none=False
    )


# 题目内容DTO
class QuestionContentSchema(Schema):
    title = fields.Str(
        required=True,
        allow_none=False,
        validate=validate.Length(min=1)
    )
    options = fields.List(
        fields.Nested(OptionSchema),
        validate=validate.Length(min=1, error="选项数组至少需要包含一个元素")
    )


# 添加题目DTO
class AddQuestionSchema(Schema):
    questionContent = fields.List(
        fields.Nested(QuestionContentSchema),
        validate=validate.Length(min=1, error="问题内容数组至少需要包含一个元素")
    )
    appId = fields.Int(
        required=True,
        allow_none=False,
        validate=validate.Range(min=1, error="AppId有误")
    )


# 题目答案DTO
class QuestionAnswerSchema(Schema):
    # 题目
    title = fields.Str(
        required=True,
        allow_none=False,
    )

    # 用户答案
    userAnswer = fields.Str(
        required=True,
        allow_none=False,
    )


class AIAddQuestionSchema(Schema):
    QuestionNumber = fields.Int(
        allow_none=True,
        required=True,
        validate=validate.Range(min=1, max=15),
    )
    OptionNumber = fields.Int(
        allow_none=True,
        required=True,
        validate=validate.Range(min=1, max=6)
    )
    AppId = fields.Int(
        required=True,
        allow_none=False,
        validate=validate.Range(min=1, error="AppId有误")
    )
    userPrompt = fields.Str(
        required=False,
        allow_none=True,
    )
    @pre_load
    def replace_none_with_default(self, data, **kwargs):
        not_none_fields_with_default = {
            'QuestionNumber': 10,
            'OptionNumber': 4,
        }
        for field, default_value in not_none_fields_with_default.items():
            if data.get(field) is None:
                data[field] = default_value
        return data


# 题目编辑DTO
class QuestionEditSchema(Schema):
    # id
    id = fields.Int(
        required=True,
        allow_none=False,
        validate=validate.Range(min=1, error="ID非法")
    )

    # 题目内容
    questionContent = fields.List(
        fields.Nested(QuestionContentSchema),
        validate=validate.Length(min=1, error="数组最少一个元素")
    )


# 题目查询DTO
class QuestionQuerySchema(Schema):
    # 题目ID
    id = fields.Int(
        allow_none=True,
        required=False,
        validate=validate.Range(min=1, error="ID非法")
    )
    # AppId
    appId = fields.Int(
        allow_none=True,
        required=True,
        validate=validate.Range(min=1, error="AppId有误")
    )
    # 题目内容
    questionContent = fields.Str(
        required=False,
        allow_none=True,
        validate=validate.Length(min=1)
    )

    # 创建用户ID
    userId = fields.Int(
        allow_none=True,
        required=False,
        validate=validate.Range(min=1, error="ID非法")
    )

    # 不需要的ID
    notId = fields.Int(
        allow_none=True,
        required=False,
        validate=validate.Range(min=1, error="ID非法")
    )

    # 页面大小
    pageSize = fields.Int(
        allow_none=True,
        default=100,
        validate=validate.Range(min=1)
    )

    # 当前页面
    current = fields.Int(
        allow_none=True,
        default=1,
        validate=validate.Range(min=1)
    )

    # 排序字段
    sortField = fields.Str(
        default='id'
    )

    # 排序方式
    sortOrder = fields.Str(
        default='desc',
        validate=validate.OneOf([SORT_ORDER_ASC, SORT_ORDER_DESC], error="排序顺序必须是asc或desc")
    )

    isDelete = fields.Int(
        allow_none=True,
        validate=validate.OneOf([0, 1]),
    )


# 题目更新DTO
class QuestionUpdateSchema(Schema):
    # 题目ID
    id = fields.Int(
        required=True,
        allow_none=False,
        validate=validate.Range(min=1, error="ID非法")
    )

    # 题目内容
    questionContent = fields.List(
        fields.Nested(QuestionContentSchema),
        validate=validate.Length(min=1, error="数组最少一个元素")
    )
